const Koa = require('koa')
const bodyparser = require('koa-bodyparser') // post参数解析
const cors = require('koa2-cors')
const app = new Koa(); // 实例化
// 引入路由
const UserRouter = require('./router/users')

// // 定义允许跨域中间件
// app.use(async (ctx, next) => {
//   // ctx request response
//   ctx.set("Access-Control-Allow-Origin", "*")
//   ctx.set("Access-Control-Allow-Credentials", "true");
//   ctx.set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,Token,Accept, Connection, User-Agent, Cookie");
//   ctx.set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
//   await next(); // 让你往下走，可以访问接口了
// })

app.use(cors())
app.use(bodyparser())
app.use(UserRouter.routes(), UserRouter.allowedMethods())

app.listen(3000, () => {
  console.log(`server port is 3000`)
})